/**
 * Syg at 17.03.2013 for Pikovskii
 */
package intf.impl;

import java.util.ArrayList;

import ru.ts.common.misc.Text;

import intf.IDataSource;
import intf.ILevel;
import intf.IPLine;

/**
 * @author Syg
 *
 */
public class DataSourceImpl implements IDataSource
{

	private PLineImpl m_border;
	private ArrayList<LevelImpl> m_levels;
	private double m_r;

	/**
	 * @param r
	 */
    public DataSourceImpl( PLineImpl border, double r )
    {
    	this( r );
    	m_border = border;
    }

	/**
	 * @param r
	 */
    public DataSourceImpl(  double r )
    {
    	m_levels = new ArrayList<LevelImpl>();
	    this.m_r = r;
    }

	/* (non-Javadoc)
	 * @see intf.IBorder#getBorder()
	 */
	@Override
	public IPLine getBorder()
	{
		return m_border;
	}

	/* (non-Javadoc)
	 * @see intf.IDataSource#getLevelCount()
	 */
	@Override
	public int getLevelCount()
	{
		return m_levels.size();
	}

	/* (non-Javadoc)
	 * @see intf.IDataSource#getLevel(int)
	 */
	@Override
	public ILevel getLevel( int levelindex )
	{
		for(ILevel l: m_levels)
			if ( l.index() == levelindex)
				return l; 
		throw new IllegalArgumentException("Designated level index " + levelindex + " not found");
	}

	/* (non-Javadoc)
	 * @see intf.IDataSource#R()
	 */
	@Override
	public double R()
	{
		return m_r;
	}
	
	public void addLevel( LevelImpl level)
	{
		int index = level.index();
		for(ILevel l: m_levels)
			if ( l.index() == index)
			{
				throw new IllegalArgumentException( "Level index " + index + " already exists in the list");
			}
		m_levels.add(level);
	}
	
	public void setBorder( PLineImpl border )
	{
		m_border = border;
	}
}
